package ldkxingzhe.l365calendar.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import ldkxingzhe.l365calendar.model.LocationEntity;

/**
 * 定位的数据层Dao部分
 * 实体使用的是Location
 * Created by xingzhe on 16/2/2.
 */
public class LocationDao {
    @SuppressWarnings("unused")
    private static final String TAG = "LocationDao";

    private L365CalendarOpenHelper helper;

    public LocationDao(Context context){
        helper = L365CalendarOpenHelper.getInstance(context);
    }

    public void insert(LocationEntity location){
        ContentValues contentValues = new ContentValues();
//        contentValues.put(L365CalendarOpenHelper.ID, location.get_id());
        contentValues.put(L365CalendarOpenHelper.CREATED, location.getCreated());
        contentValues.put(L365CalendarOpenHelper.LOCATION_LATITUDE, location.getLatitude());
        contentValues.put(L365CalendarOpenHelper.LOCATION_LONGITUDE, location.getLongitude());
        contentValues.put(L365CalendarOpenHelper.LOCATION_RADIUS, location.getRadius());
        contentValues.put(L365CalendarOpenHelper.LOCATION_LOCTYPE, location.getLocType());
        contentValues.put(L365CalendarOpenHelper.LOCATION_DESCRIBE, location.getLocationDescribe());
        contentValues.put(L365CalendarOpenHelper.LOCATION_DISTRICT, location.getDistrict());
        contentValues.put(L365CalendarOpenHelper.LOCATION_PROVINCE, location.getProvince());
        contentValues.put(L365CalendarOpenHelper.LOCATION_STREET, location.getStreet());
        contentValues.put(L365CalendarOpenHelper.LOCATION_COOR_TYPE, location.getCoorType());
        helper.getWritableDatabase().insert(L365CalendarOpenHelper.TABLE_LOCATION, null, contentValues);
    }


    public void delete(int id){
        String where = L365CalendarOpenHelper.ID + "=" + id;
        helper.getWritableDatabase().delete(L365CalendarOpenHelper.TABLE_LOCATION, where, null);
    }

    LocationEntity getLocationEntity(Cursor cursor){
        LocationEntity entity = new LocationEntity();
        entity.set_id(cursor.getInt(cursor.getColumnIndexOrThrow(L365CalendarOpenHelper.ID)));
        entity.setCoorType(cursor.getString(cursor.getColumnIndexOrThrow(L365CalendarOpenHelper.LOCATION_COOR_TYPE)));
        entity.setCreated(cursor.getLong(cursor.getColumnIndexOrThrow(L365CalendarOpenHelper.CREATED)));
        entity.setDistrict(cursor.getString(cursor.getColumnIndexOrThrow(L365CalendarOpenHelper.LOCATION_DISTRICT)));
        entity.setLatitude(cursor.getDouble(cursor.getColumnIndexOrThrow(L365CalendarOpenHelper.LOCATION_LATITUDE)));
        entity.setLongitude(cursor.getDouble(cursor.getColumnIndexOrThrow(L365CalendarOpenHelper.LOCATION_LONGITUDE)));
        entity.setLocType(cursor.getInt(cursor.getColumnIndexOrThrow(L365CalendarOpenHelper.LOCATION_LOCTYPE)));
        entity.setProvince(cursor.getString(cursor.getColumnIndexOrThrow(L365CalendarOpenHelper.LOCATION_PROVINCE)));
        entity.setStreet(cursor.getString(cursor.getColumnIndexOrThrow(L365CalendarOpenHelper.LOCATION_STREET)));
        entity.setLocationDescribe(cursor.getString(cursor.getColumnIndexOrThrow(L365CalendarOpenHelper.LOCATION_DESCRIBE)));
        entity.setRadius(cursor.getFloat(cursor.getColumnIndexOrThrow(L365CalendarOpenHelper.LOCATION_RADIUS)));
        return entity;
    }

    Cursor selectAll(){
        return helper.getReadableDatabase().rawQuery("SELECT * FROM " + L365CalendarOpenHelper.TABLE_LOCATION, null);
    }
}
